<template>
 <ContentWrap>
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="68px"
    >
      <el-form-item label="商户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          class="!w-240px"
          clearable
          placeholder="请输入商户名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺id" prop="id">
        <el-input
          v-model="queryParams.id"
          class="!w-240px"
          clearable
          placeholder="请输入店铺id"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          class="!w-240px"
          clearable
          placeholder="请输入店铺名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
            <el-form-item label="店铺手机" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          class="!w-240px"
          clearable
          placeholder="请输入店铺手机号码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺状态" prop="status">
        <el-radio-group v-model="queryParams.status" class="!w-240px">
          <el-radio value="0" size="large">启用</el-radio>
          <el-radio value="1" size="large">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
        <el-button
          v-hasPermi="['product:spu:create']"
          plain
          type="primary"
          @click="dialogVisible=true"
        >
          <Icon class="mr-5px" icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
    <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" style="width: 100%">
    <el-table-column align="center" prop="id" label="店铺id" min-width="80"  />
    <el-table-column align="center" prop="name" label="商户名称" min-width="80"  />
    <el-table-column align="center" prop="mobile" label="手机号码" min-width="80" />
    <el-table-column align="center" prop="shopName" label="店铺名称" />
    <el-table-column align="center" prop="status" label="店铺状态" >
      <template #default="scope">
        <el-tag
          :type="merchantStatusTag[scope.row.status]"
          disable-transitions
          >{{ merchantStatus[scope.row.status]}}</el-tag
        >
      </template>
    </el-table-column>
    <el-table-column align="center" prop="shopAddress" label="店铺位置" />
    <el-table-column align="center" prop="businessHours" label="营业时间" />
    <el-table-column align="center" prop="createTime" label="创建时间" />
>
    <el-table-column fixed="right" label="操作" min-width="120">
      <template #default="scope" >
        <!-- <el-button link type="success" size="small" @click="dialogMerchantVisible = true">
          绑定
        </el-button> -->
        <el-button link type="primary" size="small" @click="handleUpdate(scope.row.id)">修改</el-button>
        <el-button link type="danger" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <Pagination
    v-model:limit="queryParams.pageSize"
    v-model:page="queryParams.pageNo"
    :total="total"
    @pagination="getList"
/>
    <el-dialog v-model="dialogVisible" title="更新商户" width="400" >
      <MerchantInfo :id="merchantId" @refresh="handleQuery"  @close="dialogVisible = false"  />
    </el-dialog>
  </ContentWrap>
</template>

<script lang="ts" setup>

defineOptions({ name: 'equitment' })
interface MerchantItem {
  value: string
  label: string
}
import * as MerchantAPi from '@/api/mall/merchant/index'
import {MerchantInfo} from './component/index'

const message = useMessage() // 消息弹窗
const route = useRoute() // 路由
const { t } = useI18n() // 国际化
const { push } = useRouter() // 路由跳转
const dialogVisible = ref(false)
const loading = ref(false) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 
const merchantId=ref('')
const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  id: "" ,// 商户id
  name: "" ,// 商户名称
  mobile: "" ,// 商户手机
  shopName: "" ,// 店铺名称
  status:undefined ,// 设备状态 本地状态 0-正常 1-禁用
  createTime: undefined
}) // 查询参数
const queryFormRef = ref() // 搜索的表单Ref
const merchantStatusTag={
  0: "success",
  1: "danger",
}
const merchantStatus={
  0: "启用",
  1: "禁用",
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MerchantAPi.getMerchantPage(queryParams.value)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  if(queryFormRef.value){
    queryFormRef.value.resetFields()
  }
  handleQuery()
}


const handleUpdate=( id:string)=>{
    merchantId.value=id;
    dialogVisible.value = true;
}

const handleDelete=(id:number)=>{
     MerchantAPi.deleteMerchantById(id).then(()=>{
         message.success("删除成功")
         getList()
     }).catch((err)=>{
         message.error(err.message)
     })
}
/** 初始化 **/
onMounted(async () => {
  // 获得商品信息
  await getList()
})
</script>